package com.antexpress.dao;

import com.antexpress.pojo.ExpressDistribution;
import java.util.ArrayList;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class ExpressDistributionDAOImpl implements ExpressDistributionDAO{
    @Autowired  
    private SessionFactory sessionFactory;

    private Session getSession(){ 
        return sessionFactory.getCurrentSession();
    }
    
    @Override
    @Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_UNCOMMITTED)
    public boolean addExpressDistribution(ArrayList<ExpressDistribution> expList){
        Session session = getSession();
        boolean isSuccess = false;
        try{
            session.beginTransaction();
            for(ExpressDistribution exp:expList){
                session.save(exp);
            }
            session.getTransaction().commit();
            session.flush();
            isSuccess = true;
        }catch(Exception e){
            e.printStackTrace();
            isSuccess = false;
        }
        return isSuccess;
    }
    
}
